import {MOUNTED, MOUNTING, NOT_MOUNTED, SKIP_BECAUSE_BROKEN} from "../application/apps.helper";
import {reasonableTime} from "../application/timeouts";
import {getProps} from "./helper";
import {toUnmountPromise} from "./unmount";

export function toMountPromise(app) {
  if(app.status !== NOT_MOUNTED){
    return Promise.resolve(app);
  }

  app.status = MOUNTING;

  return reasonableTime(app.mount(getProps(app)), `app: ${app.name} mounting`, app.timeouts.mount)
    .then(() => {
      app.status = MOUNTED;
      return app;
    })
    .catch(e=>{
      // 如果app 挂载失败 那么立即执行unmount操作
      app.status = MOUNTED;
      console.error(e);
      return toUnmountPromise(app);
    });
}
